{
  "cells": [
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "\"\"\"\nin_args:\n:param test_size:FLOAT:0.2 测试数据集大小\n:param random_state:INTEGER:42 随机数\n\nout_args:\n:param train_mse:ret[\"train_mse\"] 训练集mse\n:param test_mse:ret[\"test_mse\"] 测试集mse\n:param train_r2:ret[\"train_r2\"] 训练集r2\n:param test_r2:ret[\"test_r2\"] 测试集r2\n\ndataset:\n:smart_ca_housing:/task/dataset.csv  \n\noutput:\n:/task/saved_model/\n\"\"\"\nds_ename = \"smart_ca_housing\"\nfrom bexk_da_sdk import bexk_da_repo\nbexk_da_repo.load(\"DS:PD\",ds_ename,\"./dataset.csv\")",
      "execution_count": 9,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "#输入参数\nimport os\nimport json\ntask_args=json.loads(os.getenv(\"BEDE_GLOBAL_TASK_ARGS\")) if os.getenv(\"BEDE_GLOBAL_TASK_ARGS\") is not None else {}\nparam_test_size=float(task_args.get(\"test_size\",0.2))\nparam_random_state=int(task_args.get(\"random_state\",42))\n",
      "execution_count": 4,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "import os\nimport pandas as pd\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nimport joblib\nimport shutil\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression, Ridge, Lasso\nfrom sklearn.metrics import mean_squared_error, r2_score\n#训练代码\ndef _train(data_path, test_size, random_state):\n    data = pd.read_csv(\"./dataset.csv\")\n    feature_columns = [\"MEDINC\",\"HOUSE_AGE\",\"AVE_ROOMS\",\"AVE_BEDRMS\",\"AVE_OCCUP\",\"LATITUDE\",\"LONGITUDE\",\"POPULATION\"]\n    target_column = [\"MED_HOUSE_VAL\"]\n    X = data[feature_columns]\n    Y = data[target_column]\n\n    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=random_state)\n\n    model = LinearRegression()\n    model.fit(X_train, y_train)\n    y_train_pred = model.predict(X_train)\n    y_test_pred = model.predict(X_test)\n    \n    # 评估模型\n    train_mse = mean_squared_error(y_train, y_train_pred)\n    test_mse = mean_squared_error(y_test, y_test_pred)\n    train_r2 = r2_score(y_train, y_train_pred)\n    test_r2 = r2_score(y_test, y_test_pred)\n    train_dict = {\"train_mse\":train_mse,\"test_mse\":test_mse,\"train_r2\":train_r2,\"test_r2\":test_r2}\n    \n    print(f\"训练集 MSE: {train_mse:.4f}, R²: {train_r2:.4f}\")\n    print(f\"测试集 MSE: {test_mse:.4f}, R²: {test_r2:.4f}\")\n\n    model_dir = \"./saved_model\"\n    os.makedirs(model_dir, exist_ok=True)\n\n    model_path = os.path.join(model_dir, \"linear_regression.pkl\")\n    joblib.dump(model, model_path)\n\n    return model_path,train_dict",
      "execution_count": 5,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "release_source_path,train_dict =  _train(\"./dataset.csv\", param_test_size,param_random_state)",
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": "训练集 MSE: 0.5210, R²: 0.6077\n测试集 MSE: 0.5380, R²: 0.5999\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "#输出训练指标\nret=train_dict\ntrain_mse=train_dict['train_mse']\ntest_mse=train_dict['test_mse']\ntrain_r2=train_dict['train_r2']\ntest_r2=train_dict['test_r2']",
      "execution_count": 7,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "with open('./saved_model/linear.txt', 'w', encoding='utf-8') as f:\n    f.write(\"这是过程文件\")",
      "execution_count": 12,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.8",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}